package cn.wxq.daily.empdailymgforserve.service.impl;

import cn.wxq.daily.empdailymgforserve.domain.User;
import cn.wxq.daily.empdailymgforserve.mapper.UserDao;
import cn.wxq.daily.empdailymgforserve.service.UserService;
import cn.wxq.daily.empdailymgforserve.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;



    /**
     * 密码加密
     */
    @Autowired
    BCryptPasswordEncoder encoder;

    /**
     * 根据用户名和密码登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public User findByUsernameAndPassword(String username, String password) {
        //调用持久层查询密码
        User user = userDao.findByUsername(username);
        if (user != null && encoder.matches(password, user.getPassword())) {
            return user;
        } else {
            return null;
        }


    }

    @Override
    public List<User> findAllUser(User user) {
     /*   Map<Object, Object> map = new HashMap<Object, Object>();
        if (find != null){
            map.put(1,find.getId());
        }*/
        return userDao.findAllUser(user);
    }

    @Override
    public int insertUser(User user) {
        // 新增用户信息
        //密码加密
        String newpassword = encoder.encode(user.getPassword());//加密后 的密码
        user.setPassword(newpassword);
        int rows = userDao.insertUser(user);

       /* // 新增用户岗位关联
        insertUserPost(user);
        // 新增用户与角色管理
        insertUserRole(user);*/
        return rows;
    }

    /**
     * 通过id查询用户信息
     *
     * @param userId 用户ID
     * @return
     */
    @Override
    public User selectUserById(Integer userId) {
        return userDao.selectUserById(userId);
    }

    @Override
    public List<User> findByPage(User user) {
        return userDao.findByPage(user);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public int deleteById(Integer userId) {
        return userDao.deleteById(userId);
    }
}
